package utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class ReadMap {

	public static Map<Integer,ArrayList<String>> ReadMap(String aFileName){
		try {
			Map<Integer,ArrayList<String>> map = new HashMap<Integer,ArrayList<String>>();
			map = processLineByLine(new File(aFileName), map);
			return map;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null; //OK foi para dar apenas
	}

	/** Template method that calls {@link #processLine(String)}.  */
	public final static Map<Integer,ArrayList<String>> processLineByLine(File fFile, Map<Integer,ArrayList<String>> map) throws FileNotFoundException {
		//Note that FileReader is used, not File, since File is not Closeable
		Scanner scanner = new Scanner(new FileReader(fFile));
		try {
			int line = 0; // nr da linha para colocar como chave no mapa
			while ( scanner.hasNextLine() ){
				map.put(line, processLine( scanner.nextLine(), line ));
				line++;
			}
			return map;
		}
		finally {
			//ensure the underlying stream is always closed
			//this only has any effect if the item passed to the Scanner
			//constructor implements Closeable (which it does in this case).
			scanner.close();
		}
	}

	protected static ArrayList<String> processLine(String aLine, Integer line){

		Scanner scanner = new Scanner(aLine);
		ArrayList<String> line_array = new ArrayList<String>();

		while ( scanner.hasNext() ){
			String value = scanner.next();
			line_array.add(value);
			//log("Value is : " + quote(value.trim()) );
		}
		return line_array;	
	}

	private static void log(Object aObject){
	}

	private String quote(String aText){
		String QUOTE = "'";
		return QUOTE + aText + QUOTE;
	}
}